package com.bfs.controller;

import cfk.pojo.order.CustQueryReq;
import cfk.pojo.order.CustQueryResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author baohuajie
 * @description: test
 * @date 2020/11/135:36 PM
 */
@RestController
@RequestMapping("/ribbon")
public class RibbonController {

    private final Logger log = LoggerFactory.getLogger(RibbonController.class);

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping(value = "/queryLoanByLoanId")
    public CustQueryResp queryLoanByLoanId(){
        log.info("=========当前端口号:{}=========");
        CustQueryReq queryReq = new CustQueryReq();
        queryReq.setCustId("123123");
        ResponseEntity<CustQueryResp> resp = restTemplate.postForEntity("http://CFK-SERVICE/hystrixQuery", queryReq, CustQueryResp.class);
        CustQueryResp body = resp.getBody();
        return body;
    }
    @RequestMapping(value = "/myCfkRibbonClient")
    public CustQueryResp cfkRibbonClient(){
        // 和配置文件中的CFK-SERVICE12.ribbon.listOfServers=http://localhost:9091 中的CFK-SERVICE12保持一致
        ServiceInstance instance = loadBalancerClient.choose("CFK-SERVICE12");
        int port = instance.getPort();
        CustQueryResp custQueryResp = new CustQueryResp();
        custQueryResp.setCustId(String.valueOf(port));
        return custQueryResp;


    }

}
